<?php

namespace app\admin\controller\users;

use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\User as UserModel;
use app\admin\model\Third;
use app\admin\model\WhiteList;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class Users extends Backend
{

    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
    }

    /**
     * 列表
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->field('id')
                ->where('group_id', 1)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->where('group_id', 1)
                ->limit($offset, $limit)
                ->field('id,username,mobile,avatar,joinip,status,prevtime')
                ->select();
            if ($total > 0) {
                foreach ($list as $key => $val) {
                    $list[$key]['openid'] = Third::where(['user_id' => $val['id']])->value('openid');
                }
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = User::get($ids);
        $Third = Third::where('user_id', $ids)->find();
        if ($Third) {
            $row['openid'] = $Third['openid'];
        } else {
            $row['openid'] = '';
        }
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            unset($params['openid']);
            if ($params) {
                $params = $this->preExcludeFields($params);
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 解绑用户openid
     */
    public function openid()
    {
        $id = $this->request->post('id');
        if (!$id) {
            $this->error(__('id 不能为空'));
        }

        $Third = Third::where(['user_id' => $id])->find();
        if (!$Third) {
            $this->error(__('用户不存在'));
        }
        $res = $Third::where(['user_id' => $id])->update(['openid' => '']);
        if ($res) {
            $this->success('操作成功');
        }
    }
}

